package com.rfid.rls_psw;

import android.app.Application;

import com.greendao.gen.DaoMaster;
import com.greendao.gen.DaoSession;
import com.rfid.afd.DBHelper;
import com.rfid.config.ConfigManager;
import com.rfid.config.Constant;
import com.rfid.util.BeeperUtils;
import com.rfid.util.LogUtils;
import com.rfid.util.PreferenceUtil;

import org.greenrobot.greendao.database.Database;

import java.util.Observable;

public class App extends Application {
    private static App mContext;
    private DaoSession daoSession;
    private Observable mObservable;
    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.init(this);
        mContext = this;
        mObservable = new Observable() {
            @Override
            public void notifyObservers(Object arg) {
                setChanged();
                super.notifyObservers(arg);
            }
        };
//        init();
    }

    public static App getInstance() {
        return  mContext;
    }

    public void init() {
        PreferenceUtil.init(this);
        BeeperUtils.init(this);
        BeeperUtils.setBeepMode(BeeperUtils.BeepMode.BEEP_PER_TAG);
        ConfigManager.getInstance().initConfig(Constant.CONFIG_PATH);
        Constant.init();
    }

    public void initDb() {
        DBHelper helper = new DBHelper(this,  Constant.DB_PATH);
        Database db = helper.getWritableDb();
        daoSession = new DaoMaster(db).newSession();
    }

    public Observable getObservable() {
        return mObservable;
    }

    public synchronized DaoSession getDaoSession() {
        if(daoSession == null) {
            init();
        }
        return daoSession;
    }
}
